<template>
  <div class="app-container">
    <Find :OriginalFilter="{ static: true }" :formList="formList" @findClicked="findClicked" />
    <div class="flex mt10">
      <el-card shadow="never" :body-style="{ padding: '10px' }" style="margin-right: 20px">
        <div slot="header" class="mb18 f16">
          <span style="font-weight: 600">指令资料</span>
        </div>
        <Table
          :tableData="tableData"
          :tableHeader="tableHeader"
          selection
          :TableHeight="100"
          :outTableHeight="100"
          :isLoading="isLoading"
          @currentRow="currentRow"
          @selectList="TableSelectList"
          :summaryData="summaryData"
          summary
        />
        <PageModule :PageData="{ total, buttonS: true }" @pageSizeChange="handleSizeChange" @pageChange="handleCurrentChange" />
      </el-card>

      <el-card shadow="never" :body-style="{ padding: '10px' }">
        <div slot="header" class="mb18 f16">
          <span style="font-weight: 600">预算单价明细</span>
        </div>
        <Table :tableData="tableData2" :tableHeader="tableHeader2" :TableHeight="100" :outTableHeight="100" />
        <PageModule :PageData="{ pageSizeShow: true }" />
      </el-card>
    </div>
  </div>
</template>
<script lang="ts">
import { defineComponent, ref } from 'vue'
import { ElMessage } from 'element-plus'
import { debounce } from 'lodash'
import service from '@/utils/request'
import fileRequest from '@/utils/fileRequest'
import LoginWork from '@/utils/LoginWork'
import JMessage from '@/utils/JMessage'
export default defineComponent({
  name: 'ExternalUnitPrice'
})
</script>
<script lang="ts" setup>
import { i18n, Jzhcn } from '@/i18n'
import Find from '@/components/Find/index.vue'
import Api from '@/api/GeneralManager/Factory/ExternalUnitPrice'
import { computed, onMounted, watch } from 'vue'
import { TableList, selectList, type TableListType } from '@/plugins/mixin'
const formList = ref([
  {
    type: 'input',
    inputPlaceholder: '请输入指令',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '请输入型体',
    inputValue: ''
  },
  {
    type: 'date',
    dateLabel: '下单日期',
    dateValue: ''
  },
  {
    type: 'date',
    dateLabel: '指令交期',
    dateValue: ''
  },
  {
    type: 'checkbox',
    checkboxLabel: '未审核',
    checked: true
  },
  {
    type: 'checkbox',
    checkboxLabel: '已审核',
    checked: true
  },

  {
    type: 'button',
    checkboxLabel: '打勾记录审核'
  },
  {
    type: 'button',
    checkboxLabel: '打勾取消审核'
  },
  {
    type: 'button',
    checkboxLabel: '导出当前行预算明细'
  },
  {
    type: 'button',
    checkboxLabel: '置空',
    bgColor: '#1890FF'
  }
])
const tableHeader = ref([
  {
    label: '客户',
    prop: 'customer'
  },
  {
    cfmFlag: '#00ff00',
    label: '指令',
    prop: 'lotNo'
  },
  {
    label: '型体',
    prop: 'style'
  },
  {
    label: '颜色',
    prop: 'styleColor'
  },
  {
    label: '订单数',
    prop: 'orderQty'
  },
  {
    label: '下单日期',
    prop: 'customOrderDate'
  },
  {
    label: '交期',
    prop: 'deliveryDate',
    sortable: true
  },
  {
    label: '制程',
    prop: 'process'
  },
  {
    label: '仲裁',
    prop: 'cutPrice'
  },
  {
    label: '手工',
    prop: 'craftPrice'
  },
  {
    label: '针车',
    prop: 'stitchPrice'
  },
  {
    label: '本厂单价',
    prop: 'costPrice'
  },
  {
    label: '外接单价',
    prop: 'outPrice'
  }
])
const currentPage = ref(1)
const pageSize = ref(40)
const total = ref(0)
const tableData = ref([])
const tableData2 = ref([])
const imgList = ref<string[]>([])
// 总计
interface summaryDataType {
  index: number
  num: number | undefined
}
const summaryData = ref<summaryDataType[]>([
  {
    index: 5,
    num: 0
  },
  {
    index: 12,
    num: 0
  },
  {
    index: 13,
    num: 0
  }
])
const cfmFlag = () => {
  const Unaudited = formList.value.find((item: any) => item.type === 'checkbox' && i18n.global.t(item.checkboxLabel) === '未审核')
  const Audited = formList.value.find((item: any) => item.type === 'checkbox' && i18n.global.t(item.checkboxLabel) === '已审核')
  if (!Unaudited || !Audited) return
  if (Unaudited.checked && Audited.checked) {
    return ''
  } else if (Unaudited.checked) {
    return '0'
  } else if (Audited.checked) {
    return '1'
  } else {
    ElMessage('必须至少选择一项')
    Unaudited.checked = true
    Audited.checked = true
    return ''
  }
}
const isLoading = ref(false)
const getTableList = debounce(async () => {
  isLoading.value = true
  let tableDataList = await service.put<any, any>('/pcFactorWork/listDhHead', {
    brand: '',
    deliveryEndTime: formList.value.find(
      (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '指令交期'
    )?.dateValue?.[1],
    deliveryStartTime: formList.value.find(
      (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '指令交期'
    )?.dateValue?.[0],
    lotNo: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入指令'
    )?.inputValue,
    orderEndTime: formList.value.find(
      (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '下单日期'
    )?.dateValue?.[1],
    orderStartTime: formList.value.find(
      (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '下单日期'
    )?.dateValue?.[0],
    style: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入型体'
    )?.inputValue,
    pageNum: currentPage.value,
    pageSize: pageSize.value,
    cfmFlag: cfmFlag() //	有无欠数(Y/N)	string	body	true
  })
  imgList.value = tableDataList.dtos.map((item: any) => {
    return item.photoPath
  })
  tableData.value = tableDataList.dtos
  total.value = tableDataList.total
  currentRow()
  summaryData.value[0].num = tableDataList.orderQtySum
  summaryData.value[1].num = tableDataList.costPriceSum
  summaryData.value[2].num = tableDataList.outPriceSum
  isLoading.value = false
}, 500)
// const getTableList = async () => {

//   const { Data, dataList, srcList } = await TableList(formList, Api.getTableList, currentPage.value, pageSize.value) as TableListType
//   tableData.value = dataList
//   total.value = Data.total
//   imgList.value = srcList
//   console.log(tableData.value);
// }
const getSelectList = async () => {
  await selectList(formList, Api.getSelectList)
}
onMounted(async () => {
  getTableList()
  getSelectList()
})

const handleSizeChange = (val: number) => {
  pageSize.value = val
  getTableList()
}
const handleCurrentChange = (val: number) => {
  currentPage.value = val
  getTableList()
}
watch(
  () =>
    formList.value.map((item: any) => {
      item.selectValue, item.inputValue, item.dateValue, item.checked
    }),
  () => {
    getTableList()
  }
)
// 点击按钮
const findClicked = async (content: string) => {
  if (content === '置空') {
    formList.value.forEach((item) => {
      item.inputValue = ''
      item.dateValue = ''
      item.checked = true
    })
  }
  if (content === '打勾记录审核') {
    const res = (await LoginWork.put('/pcFactorWork/auditDhDetail', { cfmFlag: 'Y', lotNoList: lotNoList.value })) as any
    if (res.code === 200) {
      JMessage.success('审核成功')
      getTableList()
    } else {
      JMessage.error(res.message)
    }
  }
  if (content === '打勾取消审核') {
    const res = (await LoginWork.put('/pcFactorWork/auditDhDetail', { cfmFlag: 'N', lotNoList: lotNoList.value })) as any
    if (res.code === 200) {
      JMessage.success('取消审核成功')
      getTableList()
    } else {
      JMessage.error(res.message)
    }
  }
  if (content === '导出当前行预算明细') {
    const res = (await fileRequest.post(`/pcFactorWork/listPcDhPriceExcel?lotNo=${currentTableRow.value.lotNo}`)) as any
    const link = document.createElement('a') // 利用a标签自定义下载文件名
    let blob = new Blob([res.data]) // 创建Blob对象，设置文件类型
    link.style.display = 'none'
    //设置连接
    link.href = window.URL.createObjectURL(blob)
    link.download = decodeURIComponent(`${res.headers['content-disposition'].split("utf-8''")[1]}`) //自定义文件名字
    document.body.appendChild(link)
    //模拟点击事件
    link.click()
    document.body.removeChild(link)
  }
}

const bgColor = computed(() => {
  tableData.value.forEach((item: any) => {
    if (item.cfmFlag === 'Y') return '#40e080'
  })
})

const tableHeader2 = ref([
  {
    label: '预算项目',
    prop: 'process'
  },
  {
    label: '单价',
    prop: 'price'
  },
  {
    label: '备注',
    prop: 'remark'
  },
  {
    label: '建档日期',
    prop: 'slipDate'
  },
  {
    label: '操作员',
    prop: 'sysUser'
  }
])
const currentTableRow = ref({}) as any
const currentRow = async (row: any = tableData.value[0] ? tableData.value[0] : {}) => {
  let { lotNo } = row
  currentTableRow.value = row
  try {
    let data = await Api.getTableList2(lotNo)
    // console.log(data);
    tableData2.value = data.dtos
  } catch (error) {
    console.log(error)
  }
}
const lotNoList = ref([])
const TableSelectList = (selectedList: any) => {
  lotNoList.value = selectedList.map((item: any) => {
    return item.lotNo
  })
}
</script>

<style lang="less" scoped>
table {
  table-layout: auto !important;
}

:deep(.el-table__header) {
  width: 100% !important;
}

:deep(.el-table__body) {
  width: 100% !important;
}
</style>
